/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.helper;

import com.nc.kyiv.yellow.sociallibrary.POJO.SessionInfo;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Evgeniy Grigorevskiy
 */
public class Sessions {

    HttpServletRequest req;
    private String[] mapKeys = {"userId", "userLogin", "userPass", "userFirstName", "userRole"};
    private String SessionInfoAttrName = "aboutUser";

    public String getSessionInfoAttrName() {
        return SessionInfoAttrName;
    }

    public void setSessionInfoAttrName(String SessionInfoAttrName) {
        this.SessionInfoAttrName = SessionInfoAttrName;
    }

    public Sessions() {
    }

    public String[] getMapKeys() {
        return mapKeys;
    }

    public void setMapKeys(String[] mapKeys) {
        this.mapKeys = mapKeys;
    }

    public HttpSession createNewSession(SessionInfo parameters, HttpSession currentSession) {
        currentSession.setAttribute(SessionInfoAttrName, parameters);
        return currentSession;
    }

    public SessionInfo getParametersFromSession(HttpSession currentSession) {
        if (currentSession.getAttribute(SessionInfoAttrName) != null) {
            SessionInfo aboutUser = (SessionInfo) currentSession.getAttribute(SessionInfoAttrName);
            return aboutUser;
        }
        return null;
    }

    public Map getMapParametersFromSession(HttpSession currentSession) {
        if (currentSession.getAttribute(SessionInfoAttrName) != null) {
            SessionInfo aboutUser = (SessionInfo) currentSession.getAttribute(SessionInfoAttrName);
            Map map = new HashMap<String, String>();
            
            map.put(mapKeys[0], aboutUser.getUserId());
            map.put(mapKeys[1], aboutUser.getUserLogin());
            map.put(mapKeys[2], aboutUser.getUserPass());
            map.put(mapKeys[3], aboutUser.getUserFirstName());
            map.put(mapKeys[4], aboutUser.getUserRole());
                    
            return map;
        }
        return null;
    }
}
